<template>
  <el-container>
    
    <!-- 头部区域 -->
    <el-header>
      <img src="" alt="" />
      <el-button plain @click="logout">退出</el-button>
    </el-header>



    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width = "isCope ? '64px':'200px'" >
          <div class="toggle-btn" 
          @click = 'toggleCollapse'>
          <i class="el-icon-s-unfold" ></i>
          </div>

        <!-- 侧边栏菜单区 -->
          <el-menu background-color="#072A24" 
          text-color = "#fff" 
          active-text-color = "#19B3B1" 
          unique-opened 
          :collapse = "isCope" 
          :collapse-transition = "false"
          router
          :default-active = "activePath"

          >


            <!-- 1级菜单 -->
            <el-submenu 
            :index="item.id+''" 
            v-for="item in menuList" 
            :key="item.id"
            
            >
              <!-- 1级菜单的模板区 -->
               <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName}}</span>
               </template>


              <!-- 2级菜单 -->
              <el-menu-item 
              :index="'/' + subItem.path" 
              v-for="subItem in item.children" 
              :key="subItem.id"
              @click = "saveNavState('/' + subItem.path)"
              >
               <template slot="title">
                  <!-- 图标 -->
                   <i :class="iconsobj[subItem.id]"></i>
                  <!-- 文本 -->
                  <span>{{ subItem.authName}}</span>
                </template>
              </el-menu-item>

              
            </el-submenu>
          </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
   data() {
      return {
         // 左侧菜单数据
         menuList: [],
         // 菜单icon
         iconsObj:{
           '125': 'el-icon-user-solid',
           '103': 'el-icon-warning',
           '101': 'el-icon-s-grid',
           '102': 'el-icon-s-order',
           '145': 'el-icon-share'
         },
         iconsobj:{
           '110': 'el-icon-s-claim',

           '111': 'el-icon-s-grid',
           '112': 'el-icon-paperclip',

           '104': 'el-icon-s-order',
           '115': 'el-icon-share',
           '121': 'el-icon-s-grid',

           '107': 'el-icon-s-cooperation',

           '146': 'el-icon-s-data',
         },
         // 是否折叠
         isCope:false,
         // 被激活后需要保存的链接地址
         activePath:'',
           
         
      }
   },
  created() {
   // getMenuList获取所有的左侧菜单
    // this.getMenuList()
  },
  methods: {
    logout() {
      // 清空
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的菜单
   //  getMenuList() {                 // 原型
   //     this.$http.get('memus')
   async getMenuList () {            
      const { data: res } = await this.$http.get('menus')
      console.log(res);    // res包含两个值，data与meta，data就是我们的所有数据，meta就是'获取菜单列表成功'.为将数据渲染出来，我们在上面data写返回menulist
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 菜单折叠按钮
    toggleCollapse() {
      this.isCope = !this.isCope
    },
    // 保存链接激活状态
    saveNavState(activePath) {
      // 理解：当点击二级菜单的时候立即保存活动路径，每次新的点击都将当前activePath的this指向上一个activePath，这就能实现保存的功能
      window.sessionStorage.setItem('activePath', activePath) //前面为键，后面为值 activePath：活动路径
      this.activePath = activePath
    }
  },
  mounted() {
    // 注意，执行getMenuList操作写在created与写在mounted的区别，created是在页面加载完成后执行，mounted是在完成挂载后执行，相对来说mounted算是一个优化
    this.getMenuList()
  }
};
</script>

<style scoped lang="less">
.el-header {
  background-color: #072A24;
  color: #fff;
  display: flex;
  justify-content: space-between;
  text-align: center;
  align-items: center;
  line-height: 60px;
  > div {
    display: flex;
    align-items: center;
  }
}

.el-aside {
  background-color: #072A24;
  color: #fff;
  text-align: center;
  line-height: 200px;
  :hover{
    color: #19B3B1;
  }
  .el-menu{
    border-right: none;
    // :hover{
    //   font-size: 15px;
    //   width: 203px;
    // }
  }
}

.el-menu-item{
  display: block;
  padding-left: 80px !important;
}

.el-main {
  background-color: #fff;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  height: 100%;
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
  padding-left: 50px;
}

.toggle-btn{
  line-height: 20px;
  font-size: 25px;
  background: #4A5064;
  cursor: pointer;
}

.el-container.is-vertical {
  height: 100%;
}
</style>
